//
//  HomepageCell.m
//  ppkc2016
//
//  Created by Laura on 2017/2/4.
//  Copyright © 2017年 拼凑的断音. All rights reserved.
//

#import "HomepageCell.h"
#import "XXTool.h"

@interface HomepageCell ()

@property (nonatomic) UIImageView *titleImage1;

@property (nonatomic) UILabel *contentLabel1;

@property (nonatomic) UIView *rightLine1;

@property (nonatomic) UIView *bottomLine1;

@property (nonatomic) UIImageView *titleImage2;

@property (nonatomic) UILabel *contentLabel2;

@property (nonatomic) UIView *rightLine2;

@property (nonatomic) UIView *bottomLine2;

@property (nonatomic) UIImageView *titleImage3;

@property (nonatomic) UILabel *contentLabel3;

@property (nonatomic) UIView *rightLine3;

@property (nonatomic) UIView *bottomLine3;

@property (nonatomic) UIImageView *titleImage4;

@property (nonatomic) UILabel *contentLabel4;

@property (nonatomic) UIView *bottomLine4;

@property (nonatomic) UIView *view1;

@property (nonatomic) UIView *view2;

@property (nonatomic) UIView *view3;

@property (nonatomic) UIView *view4;

@property (nonatomic) UIView *messagePoint;

@property (nonatomic) UILabel *messageCountLabel;


@end

@implementation HomepageCell

    
-(UIView*) view1
{
    if(_view1 == nil){
        _view1=[[UIView alloc] init];
        [self addSubview:_view1];
    }
    return _view1;
}

-(UIView*)messagePoint
{
    if (_messagePoint==nil)
    {
        _messagePoint = [[UIView alloc] init];
        _messagePoint.backgroundColor = [UIColor colorWithRed:192/255.0 green:7/255.0 blue:18/255.0 alpha:1.0];
        _messagePoint.clipsToBounds = YES;
        [self.view1 addSubview:_messagePoint];
        
        _messageCountLabel = [[UILabel alloc] init];
        
        _messageCountLabel.backgroundColor = [UIColor clearColor];
        _messageCountLabel.textColor = [UIColor whiteColor];
        _messageCountLabel.font = [XXTool systemFontOfSize:8];
        _messageCountLabel.textAlignment = NSTextAlignmentCenter;
        [_messagePoint addSubview:_messageCountLabel];
    }
    return _messagePoint;
}

-(UIView*) view2
{
    if(_view2 == nil){
        _view2=[[UIView alloc] init];
        [self addSubview:_view2];
    }
    return _view2;
}

-(UIView*) view3
{
    if(_view3 == nil){
        _view3=[[UIView alloc] init];
        [self addSubview:_view3];
    }
    return _view3;
}

-(UIView*) view4
{
    if(_view4 == nil){
        _view4=[[UIView alloc] init];
        [self addSubview:_view4];
    }
    return _view4;
}

-(UIImageView*) titleImage1
{
    if(_titleImage1 == nil){
        _titleImage1=[[UIImageView alloc] init];
        _titleImage1.contentMode=UIViewContentModeCenter;
       [self.view1 addSubview:_titleImage1];
    }
    return _titleImage1;
}

-(UIImageView*) titleImage2
{
    if(_titleImage2 == nil){
        _titleImage2=[[UIImageView alloc] init];
        _titleImage2.contentMode=UIViewContentModeCenter;
        [self.view2 addSubview:_titleImage2];
    }
    return _titleImage2;
}

-(UIImageView*) titleImage3
{
    if(_titleImage3 == nil){
        _titleImage3=[[UIImageView alloc] init];
        _titleImage3.contentMode=UIViewContentModeCenter;
        [self.view3 addSubview:_titleImage3];
    }
    return _titleImage3;
}

-(UIImageView*) titleImage4
{
    if(_titleImage4 == nil){
        _titleImage4=[[UIImageView alloc] init];
        _titleImage4.contentMode=UIViewContentModeCenter;
        [self.view4 addSubview:_titleImage4];
    }
    return _titleImage4;
}

-(UILabel*) contentLabel1
{
    if(_contentLabel1 == nil){
        _contentLabel1 = [[UILabel alloc] init];
        _contentLabel1.textColor = [XXTool colorWithProject];
        _contentLabel1.font = [XXTool systemFontOfSize:14];
        _contentLabel1.textAlignment = NSTextAlignmentCenter;
        _contentLabel1.numberOfLines = 0;
        [self.view1 addSubview:_contentLabel1];
    }
    return _contentLabel1;
}

-(UILabel*) contentLabel2
{
    if(_contentLabel2 == nil){
        _contentLabel2 = [[UILabel alloc] init];
        _contentLabel2.textColor = [XXTool colorWithProject];
        _contentLabel2.font = [XXTool systemFontOfSize:14];
        _contentLabel2.textAlignment = NSTextAlignmentCenter;
        _contentLabel2.numberOfLines = 0;
        [self.view2 addSubview:_contentLabel2];
    }
    return _contentLabel2;
}

-(UILabel*) contentLabel3
{
    if(_contentLabel3 == nil){
        _contentLabel3 = [[UILabel alloc] init];
        _contentLabel3.textColor = [XXTool colorWithProject];
        _contentLabel3.font = [XXTool systemFontOfSize:14];
        _contentLabel3.textAlignment = NSTextAlignmentCenter;
        _contentLabel3.numberOfLines = 0;
        [self.view3 addSubview:_contentLabel3];
    }
    return _contentLabel1;
}

-(UILabel*) contentLabel4
{
    if(_contentLabel4 == nil){
        _contentLabel4 = [[UILabel alloc] init];
        _contentLabel4.textColor = [XXTool colorWithProject];
        _contentLabel4.font = [XXTool systemFontOfSize:14];
        _contentLabel4.textAlignment = NSTextAlignmentCenter;
        _contentLabel4.numberOfLines = 0;
        [self.view4 addSubview:_contentLabel4];
    }
    return _contentLabel4;
}

-(UIView*) rightLine1
{
    if(_rightLine1 == nil){
        _rightLine1 = [[UIView alloc] init];
        _rightLine1.backgroundColor = [XXTool colorWithHexString:@"ffffff"];
        [self addSubview:_rightLine1];
    }
    return _rightLine1;
}

-(UIView*) rightLine2
{
    if(_rightLine2 == nil){
        _rightLine2 = [[UIView alloc] init];
        _rightLine2.backgroundColor = [XXTool colorWithHexString:@"ffffff"];
        [self addSubview:_rightLine2];
    }
    return _rightLine2;
}

-(UIView*) rightLine3
{
    if(_rightLine3 == nil){
        _rightLine3 = [[UIView alloc] init];
        _rightLine3.backgroundColor = [XXTool colorWithHexString:@"ffffff"];
        [self addSubview:_rightLine3];
    }
    return _rightLine3;
}

-(UIView*) bottomLine1
{
    if(_bottomLine1 == nil){
        _bottomLine1 = [[UIView alloc] init];
        _bottomLine1.backgroundColor = [XXTool colorWithHexString:@"ffffff"];
        [self addSubview:_bottomLine1];
    }
    return _bottomLine1;
}

-(UIView*) bottomLine2
{
    if(_bottomLine2 == nil){
        _bottomLine2 = [[UIView alloc] init];
        _bottomLine2.backgroundColor = [XXTool colorWithHexString:@"ffffff"];
        [self addSubview:_bottomLine2];
    }
    return _bottomLine2;
}

-(UIView*) bottomLine3
{
    if(_bottomLine3 == nil){
        _bottomLine3 = [[UIView alloc] init];
        _bottomLine3.backgroundColor = [XXTool colorWithHexString:@"ffffff"];
        [self addSubview:_bottomLine3];
    }
    return _bottomLine3;
}

-(UIView*) bottomLine4
{
    if(_bottomLine4 == nil){
        _bottomLine4 = [[UIView alloc] init];
        _bottomLine4.backgroundColor = [XXTool colorWithHexString:@"ffffff"];
        [self addSubview:_bottomLine4];
    }
    return _bottomLine4;
}

-(void)initParameter
{
    
}

-(void)initUI
{
    [self view1];
    [self view2];
    [self view3];
    [self view4];
    
    [self titleImage1];
    [self titleImage2];
    [self titleImage3];
    [self titleImage4];
    [self contentLabel1];
    [self contentLabel2];
    [self contentLabel3];
    [self contentLabel4];
    [self rightLine1];
    [self rightLine2];
    [self rightLine3];
    [self bottomLine1];
    [self bottomLine2];
    [self bottomLine3];
    [self bottomLine4];
    
    
    self.backgroundColor=[UIColor clearColor];
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    
}

-(void)refreshLayout:(CGRect)frame
{
    _view1.frame=CGRectMake(0, 0, frame.size.width/4, 100);
    _view2.frame=CGRectMake(0+frame.size.width/4, 0, frame.size.width/4, 100);
    _view3.frame=CGRectMake(0+frame.size.width/4+frame.size.width/4, 0, frame.size.width/4, 100);
    _view4.frame=CGRectMake(0+frame.size.width/4+frame.size.width/4+frame.size.width/4, 0, frame.size.width/4, 100);
    
    _titleImage1.frame=CGRectMake((_view1.frame.size.width-54)/2, 8, 54, 54);
    _contentLabel1.frame=CGRectMake(2, _titleImage1.xx_bottom-20, _view1.frame.size.width - 4, _view1.xx_height-_titleImage1.xx_bottom+20);
    _rightLine1.frame=CGRectMake(frame.size.width/4, 0, 1, 100);
    _bottomLine1.frame=CGRectMake(0, 100, frame.size.width/4, 1);
    
    _titleImage2.frame=CGRectMake((_view2.frame.size.width-54)/2, 8, 54, 54);
    _contentLabel2.frame=CGRectMake(2, _contentLabel1.xx_origin.y,_contentLabel1.frame.size.width ,_contentLabel1.frame.size.height);
    _rightLine2.frame=CGRectMake(frame.size.width/4+frame.size.width/4, 0, 1, 100);
    _bottomLine2.frame=CGRectMake(0+frame.size.width/4, 100, frame.size.width/4, 1);
    
    _titleImage3.frame=CGRectMake((_view3.frame.size.width-54)/2, 8, 54, 54);
    _contentLabel3.frame=CGRectMake(2, _contentLabel1.xx_origin.y, _contentLabel1.frame.size.width, _contentLabel1.frame.size.height);
    _rightLine3.frame=CGRectMake(frame.size.width/4+frame.size.width/4+frame.size.width/4, 0, 1, 100);
    _bottomLine3.frame=CGRectMake(0+frame.size.width/4+frame.size.width/4, 100, frame.size.width/4, 1);
    
    _titleImage4.frame=CGRectMake((_view4.frame.size.width-54)/2, 8, 54, 54);
    _contentLabel4.frame=CGRectMake(2, _contentLabel1.xx_origin.y, _contentLabel1.frame.size.width, _contentLabel1.frame.size.height);
    _bottomLine4.frame=CGRectMake(0+frame.size.width/4+frame.size.width/4+frame.size.width/4, 100, frame.size.width/4, 1);

    if (!_messagePoint.hidden)
    {
        _messagePoint.frame = CGRectMake(_titleImage1.frame.origin.x+_titleImage1.frame.size.width+_titleImage1.frame.origin.x-10.5-3
                                         , _titleImage1.frame.origin.y-6
                                         , 10.5
                                         , 10.5);
        _messagePoint.layer.cornerRadius = _messagePoint.frame.size.width/2;
        
        _messageCountLabel.frame = CGRectMake(0, 0, _messagePoint.frame.size.width, _messagePoint.frame.size.height);
    }
    
}

-(void)setcellWithHomepageData:(HomepageModel*)data
{
    if(![XXTool didCurrentLanguageChinese]){
        _contentLabel1.font = [XXTool systemFontOfSize:12];
        _contentLabel2.font = [XXTool systemFontOfSize:12];
        _contentLabel3.font = [XXTool systemFontOfSize:12];
        _contentLabel4.font = [XXTool systemFontOfSize:12];
    }
    
    _titleImage1.image=[[UIImage imageNamed:data.mode_image1] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    _titleImage2.image=[[UIImage imageNamed:data.mode_image2] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    _titleImage3.image=[[UIImage imageNamed:data.mode_image3] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    _titleImage4.image=[[UIImage imageNamed:data.mode_image4] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    _titleImage1.tintColor = [XXTool colorWithProject];
    _titleImage2.tintColor = [XXTool colorWithProject];
    _titleImage3.tintColor = [XXTool colorWithProject];
    _titleImage4.tintColor = [XXTool colorWithProject];
    
    [_contentLabel1 setText:NSLocalizedString(data.mode_title1, nil)];
    [_contentLabel2 setText:NSLocalizedString(data.mode_title2, nil)];
    [_contentLabel3 setText:NSLocalizedString(data.mode_title3, nil)];
    [_contentLabel4 setText:NSLocalizedString(data.mode_title4, nil)];
    
    _view1.tag=data.flag1;
    _view2.tag=data.flag2;
    _view3.tag=data.flag3;
    _view4.tag=data.flag4;
    
    UITapGestureRecognizer* gesture1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonTapped:)];
    [_view1 addGestureRecognizer:gesture1];
    
    UITapGestureRecognizer* gesture2=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonTapped:)];
    [_view2 addGestureRecognizer:gesture2];
    
    UITapGestureRecognizer* gesture3=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonTapped:)];
    [_view3 addGestureRecognizer:gesture3];
    
    UITapGestureRecognizer* gesture4=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonTapped:)];
    [_view4 addGestureRecognizer:gesture4];
    
    if (!data.messageVaild)
    {
        self.messagePoint.hidden = NO;
    }
    else
    {
        self.messagePoint.hidden = YES;
    }
    if (![data.messageCount isEqualToString:@""])
    {
        _messageCountLabel.hidden = NO;
        _messageCountLabel.text = data.messageCount;
    }
    else
    {
        _messageCountLabel.hidden = YES;
    }
    
    [self refreshLayout:self.frame];
}

- (void)buttonTapped: (UITapGestureRecognizer *)gr
{
     /*
      * 0首页 1简介 2会刊 3商贸配对 4消息 5收藏 6扫描二维码 7展馆地图 8联系我们 9展位登记 10观众预登记 11我
      */
    UIView*  view=gr.view;
    NSString* msg=@"";
    switch (view.tag) {
        case 0:  // 首页
            msg=@"首页";
            break;
        case 1:  // 简介
            msg=@"简介";
            break;
        case 2:  // 会刊
            msg=@"会刊";
            break;
        case 3:  // 商贸配对
            msg=@"商贸配对";
            break;
        case 4:  // 消息
            msg=@"消息";
            break;
        case 5:  // 收藏
            msg=@"收藏";
            break;
        case 6:  // 扫描二维码
            msg=@"扫描二维码";
            break;
        case 7:  // 展馆地图
            msg=@"展馆地图";
            break;
        case 8:  // 联系我们
            msg=@"联系我们";
            break;
        case 9:  // 展位登记
            msg=@"展位登记";
            break;
        case 10:  // 观众预登记
            msg=@"观众预登记";
            break;
        case 11:  // 我
            msg=@"我";
            break;
            
        default:
            break;
    }
    NSLog(@"点击的  %@",msg);
      //MyLog(@"%d", sender.tag);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"successclick" object:nil userInfo:@{@"tag":[NSNumber numberWithInteger:view.tag]}];

}

+(CGFloat)getHeightForHomepageCell:(HomepageModel *)data
{
    return 120+[XXTool getTextHeightByText:data.mode_title1
                                     Font:[XXTool systemFontOfSize:14]
                                    Width:[UIScreen mainScreen].bounds.size.width]+25;
}


@end
